import Vue from 'vue'
import axios from 'axios'
import Qs from 'qs'
//基地址
const baseUrl = 'https://www.vue-js.com/api/v1';

//首页列表
export const HomeList = function () {
  return new Promise((resolve, reject) => {
    axios.get(`${baseUrl}/topics`)
      .then(res => resolve(res.data))
      .catch(res => reject(res))
  })
}

//tab分类
export const HomeListTab = function (tab) {
  return new Promise((resolve,reject)=> {
    axios.get(`${baseUrl}/topics/?tab=${tab}`)
      .then(res=> resolve(res.data))
      .catch(res => reject(res))
  })
}

//用户详情信息
export const UserInfo = function (loginname) {
  return new Promise((resolve, reject) => {
    axios.get(`${baseUrl}/user/${loginname}`)
      .then(res => resolve(res.data))
      .catch(res => reject(res))
  })
}

//用户列表详情
export const ListInfo = function (id) {
  return new Promise((resolve , reject) =>{
    axios.get(`${baseUrl}/topic/${id}`)
      .then(res => resolve(res.data))
      .catch(res => reject(res))
  })

}

//验证用户信息
export const AccessToken = function(accesstoken){
  return new Promise((resolve, reject) => {
    let data = {
      accesstoken
    }
    axios({
      method: 'post',
      url: `${baseUrl}/accesstoken`,
      data: Qs.stringify(data)
    })
      .then(res => resolve(res.data))
      .catch(res => reject(res))
  })

}

//用户收藏信息
export const Collect = function(accesstoken,topic_id){
  return new Promise((resolve, reject) => {
    let data = {
      accesstoken,
      topic_id
    }
    axios({
      method: 'post',
      url: `${baseUrl}/topic/collect`,
      data: Qs.stringify(data)
    })
      .then(res => resolve(res.data))
      .catch(res => reject(res))
  })

}

//用户点赞
export const Ups = function (accesstoken,id) {
  return new Promise((resolve,reject)=>{
    let data = {
      accesstoken,
      id
    }
    axios({
      method: 'post',
      url: `${baseUrl}/reply/${id}/ups`,
      data: Qs.stringify(data)
    })
      .then(res=> resolve(res.data))
      .catch(res=> reject(res))
  })

}
